home *** CD-ROM | disk | FTP | other *** search
/ Super PC 33 / Super PC 33 (Shareware).iso / spc / sonido / timidity / source / resample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-03  |  15.6 KB  |  741 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <titoivon@snakemail.hut.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     resample.c
  21. */
  22.  
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include <malloc.h>
  26.  
  27. #include "config.h"
  28. #include "common.h"
  29. #ifdef __WIN32__
  30. #include "instrume.h"
  31. #else
  32. #include "instruments.h"
  33. #endif
  34. #include "playmidi.h"
  35. #include "output.h"
  36. #include "controls.h"
  37. #include "tables.h"
  38. #include "resample.h"
  39.  
  40. #ifdef LINEAR_INTERPOLATION
  41. # if defined(LOOKUP_HACK) && defined(LOOKUP_INTERPOLATION)
  42. #   define RESAMPLATION \
  43.        v1=src[ofs>>FRACTION_BITS];\
  44.        v2=src[(ofs>>FRACTION_BITS)+1];\
  45.        *dest++ = v1 + (iplookup[(((v2-v1)<<5) & 0x03FE0) | \
  46.            ((ofs & FRACTION_MASK) >> (FRACTION_BITS-5))]);
  47. # else
  48. #   define RESAMPLATION \
  49.       v1=src[ofs>>FRACTION_BITS];\
  50.       v2=src[(ofs>>FRACTION_BITS)+1];\
  51.       *dest++ = v1 + (((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
  52. # endif
  53. #  define INTERPVARS sample_t v1, v2
  54. #else
  55. /* Earplugs recommended for maximum listening enjoyment */
  56. #  define RESAMPLATION *dest++=src[ofs>>FRACTION_BITS];
  57. #  define INTERPVARS
  58. #endif
  59.  
  60. #define FINALINTERP if (ofs == le) *dest++=src[ofs>>FRACTION_BITS];
  61. /* So it isn't interpolation. At least it's final. */
  62.  
  63. static sample_t resample_buffer[AUDIO_BUFFER_SIZE];
  64.  
  65. /*************** resampling with fixed increment *****************/
  66.  
  67. static sample_t *rs_plain(int v, int32 *countptr)
  68. {
  69.  
  70.   /* Play sample until end, then free the voice. */
  71.  
  72.   INTERPVARS;
  73.   Voice 
  74.     *vp=&voice[v];
  75.   sample_t 
  76.     *dest=resample_buffer,
  77.     *src=vp->sample->data;
  78.   int32 
  79.     ofs=vp->sample_offset,
  80.     incr=vp->sample_increment,
  81.     le=vp->sample->data_length,
  82.     count=*countptr;
  83.  
  84. #ifdef PRECALC_LOOPS
  85.   int32 i;
  86.  
  87.   if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
  88.  
  89.   /* Precalc how many times we should go through the loop.
  90.      NOTE: Assumes that incr > 0 and that ofs <= le */
  91.   i = (le - ofs) / incr + 1;
  92.  
  93.   if (i > count)
  94.     {
  95.       i = count;
  96.       count = 0;
  97.     } 
  98.   else count -= i;
  99.  
  100.   while (i--) 
  101.     {
  102.       RESAMPLATION;
  103.       ofs += incr;
  104.     }
  105.  
  106.   if (ofs >= le) 
  107.     {
  108.       FINALINTERP;
  109.       vp->status=VOICE_FREE;
  110.       ctl->note(v);
  111.       *countptr-=count+1;
  112.     }
  113.  
  114. #else /* PRECALC_LOOPS */
  115.     while (count--)
  116.     {
  117.       RESAMPLATION;
  118.       ofs += incr;
  119.       if (ofs >= le)
  120.     {
  121.       FINALINTERP;
  122.       vp->status=VOICE_FREE;
  123.        ctl->note(v);
  124.       *countptr-=count+1;
  125.       break;
  126.     }
  127.     }
  128. #endif /* PRECALC_LOOPS */
  129.   
  130.   vp->sample_offset=ofs; /* Update offset */
  131.   return resample_buffer;
  132. }
  133.  
  134. static sample_t *rs_loop(Voice *vp, int32 count)
  135. {
  136.  
  137.   /* Play sample until end-of-loop, skip back and continue. */
  138.  
  139.   INTERPVARS;
  140.   int32 
  141.     ofs=vp->sample_offset, 
  142.     incr=vp->sample_increment,
  143.     le=vp->sample->loop_end, 
  144.     ll=le - vp->sample->loop_start;
  145.   sample_t
  146.     *dest=resample_buffer,
  147.     *src=vp->sample->data;
  148.  
  149. #ifdef PRECALC_LOOPS
  150.   int32 i;
  151.   
  152.   while (count) 
  153.     {
  154.       if (ofs >= le)
  155.     /* NOTE: Assumes that ll > incr and that incr > 0. */
  156.     ofs -= ll;
  157.       /* Precalc how many times we should go through the loop */
  158.       i = (le - ofs) / incr + 1;
  159.       if (i > count) 
  160.     {
  161.       i = count;
  162.       count = 0;
  163.     } 
  164.       else count -= i;
  165.       while (i--) 
  166.     {
  167.       RESAMPLATION;
  168.       ofs += incr;
  169.     }
  170.     }
  171. #else
  172.   while (count--)
  173.     {
  174.       RESAMPLATION;
  175.       ofs += incr;
  176.       if (ofs>=le)
  177.     ofs -= ll; /* Hopefully the loop is longer than an increment. */
  178.     }
  179. #endif
  180.  
  181.   vp->sample_offset=ofs; /* Update offset */
  182.   return resample_buffer;
  183. }
  184.  
  185. static sample_t *rs_bidir(Voice *vp, int32 count)
  186. {
  187.   INTERPVARS;
  188.   int32 
  189.     ofs=vp->sample_offset,
  190.     incr=vp->sample_increment,
  191.     le=vp->sample->loop_end,
  192.     ls=vp->sample->loop_start;
  193.   sample_t 
  194.     *dest=resample_buffer, 
  195.     *src=vp->sample->data;
  196.  
  197. #ifdef PRECALC_LOOPS
  198.   int32
  199.     le2 = le<<1, 
  200.     ls2 = ls<<1,
  201.     i;
  202.   /* Play normally until inside the loop region */
  203.  
  204.   if (ofs <= ls) 
  205.     {
  206.       /* NOTE: Assumes that incr > 0, which is NOT always the case
  207.      when doing bidirectional looping.  I have yet to see a case
  208.      where both ofs <= ls AND incr < 0, however. */
  209.       i = (ls - ofs) / incr + 1;
  210.       if (i > count) 
  211.     {
  212.       i = count;
  213.       count = 0;
  214.     } 
  215.       else count -= i;
  216.       while (i--) 
  217.     {
  218.       RESAMPLATION;
  219.       ofs += incr;
  220.     }
  221.     }
  222.  
  223.   /* Then do the bidirectional looping */
  224.   
  225.   while(count) 
  226.     {
  227.       /* Precalc how many times we should go through the loop */
  228.       i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
  229.       if (i > count) 
  230.     {
  231.       i = count;
  232.       count = 0;
  233.     } 
  234.       else count -= i;
  235.       while (i--) 
  236.     {
  237.       RESAMPLATION;
  238.       ofs += incr;
  239.     }
  240.       if (ofs>=le) 
  241.     {
  242.       /* fold the overshoot back in */
  243.       ofs = le2 - ofs;
  244.       incr *= -1;
  245.     } 
  246.       else if (ofs <= ls) 
  247.     {
  248.       ofs = ls2 - ofs;
  249.       incr *= -1;
  250.     }
  251.     }
  252.  
  253. #else /* PRECALC_LOOPS */
  254.   /* Play normally until inside the loop region */
  255.  
  256.   if (ofs < ls)
  257.     {
  258.       while (count--)
  259.     {
  260.       RESAMPLATION;
  261.       ofs += incr;
  262.       if (ofs>=ls)
  263.         break;
  264.     }
  265.     }
  266.  
  267.   /* Then do the bidirectional looping */
  268.  
  269.   if (count>0)
  270.     while (count--)
  271.       {
  272.     RESAMPLATION;
  273.     ofs += incr;
  274.     if (ofs>=le)
  275.       {
  276.         /* fold the overshoot back in */
  277.         ofs = le - (ofs - le);
  278.         incr = -incr;
  279.       }
  280.     else if (ofs <= ls)
  281.       {
  282.         ofs = ls + (ls - ofs);
  283.         incr = -incr;
  284.       }
  285.       }  
  286. #endif /* PRECALC_LOOPS */
  287.   vp->sample_increment=incr;
  288.   vp->sample_offset=ofs; /* Update offset */
  289.   return resample_buffer;
  290. }
  291.  
  292. /*********************** vibrato versions ***************************/
  293.  
  294. /* We only need to compute one half of the vibrato sine cycle */
  295. static int vib_phase_to_inc_ptr(int phase)
  296. {
  297.   if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
  298.     return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
  299.   else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
  300.     return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
  301.   else
  302.     return phase-VIBRATO_SAMPLE_INCREMENTS/2;
  303. }
  304.  
  305. static int32 update_vibrato(Voice *vp, int sign)
  306. {
  307.   int32 depth;
  308.   int phase, pb;
  309.   double a;
  310.  
  311.   if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
  312.     vp->vibrato_phase=0;
  313.   phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
  314.   
  315.   if (vp->vibrato_sample_increment[phase])
  316.     {
  317.       if (sign)
  318.     return -vp->vibrato_sample_increment[phase];
  319.       else
  320.     return vp->vibrato_sample_increment[phase];
  321.     }
  322.  
  323.   /* Need to compute this sample increment. */
  324.     
  325.   depth=vp->sample->vibrato_depth<<7;
  326.  
  327.   if (vp->vibrato_sweep)
  328.     {
  329.       /* Need to update sweep */
  330.       vp->vibrato_sweep_position += vp->vibrato_sweep;
  331.       if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
  332.     vp->vibrato_sweep=0;
  333.       else
  334.     {
  335.       /* Adjust depth */
  336.       depth *= vp->vibrato_sweep_position;
  337.       depth >>= SWEEP_SHIFT;
  338.     }
  339.     }
  340.  
  341.   a = FSCALE(((double)(vp->sample->sample_rate) *
  342.           (double)(vp->frequency)) /
  343.          ((double)(vp->sample->root_freq) *
  344.           (double)(play_mode->rate)),
  345.          FRACTION_BITS);
  346.  
  347.   pb=(int)((sine(vp->vibrato_phase * 
  348.          (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
  349.         * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
  350.  
  351.   if (pb<0)
  352.     {
  353.       pb=-pb;
  354.       a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
  355.     }
  356.   else
  357.     a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
  358.   
  359.   /* If the sweep's over, we can store the newly computed sample_increment */
  360.   if (!vp->vibrato_sweep)
  361.     vp->vibrato_sample_increment[phase]=(int32) a;
  362.  
  363.   if (sign)
  364.     a = -a; /* need to preserve the loop direction */
  365.  
  366.   return (int32) a;
  367. }
  368.  
  369. static sample_t *rs_vib_plain(int v, int32 *countptr)
  370. {
  371.  
  372.   /* Play sample until end, then free the voice. */
  373.  
  374.   INTERPVARS;
  375.   Voice *vp=&voice[v];
  376.   sample_t 
  377.     *dest=resample_buffer, 
  378.     *src=vp->sample->data;
  379.   int32 
  380.     le=vp->sample->data_length,
  381.     ofs=vp->sample_offset, 
  382.     incr=vp->sample_increment, 
  383.     count=*countptr;
  384.   int 
  385.     cc=vp->vibrato_control_counter;
  386.  
  387.   /* This has never been tested */
  388.  
  389.   if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
  390.  
  391.   while (count--)
  392.     {
  393.       if (!cc--)
  394.     {
  395.       cc=vp->vibrato_control_ratio;
  396.       incr=update_vibrato(vp, 0);
  397.     }
  398.       RESAMPLATION;
  399.       ofs += incr;
  400.       if (ofs >= le)
  401.     {
  402.       FINALINTERP;
  403.       vp->status=VOICE_FREE;
  404.        ctl->note(v);
  405.       *countptr-=count+1;
  406.       break;
  407.     }
  408.     }
  409.   
  410.   vp->vibrato_control_counter=cc;
  411.   vp->sample_increment=incr;
  412.   vp->sample_offset=ofs; /* Update offset */
  413.   return resample_buffer;
  414. }
  415.  
  416. static sample_t *rs_vib_loop(Voice *vp, int32 count)
  417. {
  418.  
  419.   /* Play sample until end-of-loop, skip back and continue. */
  420.   
  421.   INTERPVARS;
  422.   int32 
  423.     ofs=vp->sample_offset, 
  424.     incr=vp->sample_increment, 
  425.     le=vp->sample->loop_end,
  426.     ll=le - vp->sample->loop_start;
  427.   sample_t 
  428.     *dest=resample_buffer, 
  429.     *src=vp->sample->data;
  430.   int 
  431.     cc=vp->vibrato_control_counter;
  432.  
  433. #ifdef PRECALC_LOOPS
  434.   int32 i;
  435.   int
  436.     vibflag=0;
  437.  
  438.   while (count) 
  439.     {
  440.       /* Hopefully the loop is longer than an increment */
  441.       if(ofs >= le)
  442.     ofs -= ll;
  443.       /* Precalc how many times to go through the loop, taking
  444.      the vibrato control ratio into account this time. */
  445.       i = (le - ofs) / incr + 1;
  446.       if(i > count) i = count;
  447.       if(i > cc)
  448.     {
  449.       i = cc;
  450.       vibflag = 1;
  451.     } 
  452.       else cc -= i;
  453.       count -= i;
  454.       while(i--) 
  455.     {
  456.       RESAMPLATION;
  457.       ofs += incr;
  458.     }
  459.       if(vibflag) 
  460.     {
  461.       cc = vp->vibrato_control_ratio;
  462.       incr = update_vibrato(vp, 0);
  463.       vibflag = 0;
  464.     }
  465.     }
  466.  
  467. #else /* PRECALC_LOOPS */
  468.   while (count--)
  469.     {
  470.       if (!cc--)
  471.     {
  472.       cc=vp->vibrato_control_ratio;
  473.       incr=update_vibrato(vp, 0);
  474.     }
  475.       RESAMPLATION;
  476.       ofs += incr;
  477.       if (ofs>=le)
  478.     ofs -= ll; /* Hopefully the loop is longer than an increment. */
  479.     }
  480. #endif /* PRECALC_LOOPS */
  481.  
  482.   vp->vibrato_control_counter=cc;
  483.   vp->sample_increment=incr;
  484.   vp->sample_offset=ofs; /* Update offset */
  485.   return resample_buffer;
  486. }
  487.  
  488. static sample_t *rs_vib_bidir(Voice *vp, int32 count)
  489. {
  490.   INTERPVARS;
  491.   int32 
  492.     ofs=vp->sample_offset, 
  493.     incr=vp->sample_increment,
  494.     le=vp->sample->loop_end, 
  495.     ls=vp->sample->loop_start;
  496.   sample_t 
  497.     *dest=resample_buffer, 
  498.     *src=vp->sample->data;
  499.   int 
  500.     cc=vp->vibrato_control_counter;
  501.  
  502. #ifdef PRECALC_LOOPS
  503.   int32
  504.     le2=le<<1,
  505.     ls2=ls<<1,
  506.     i;
  507.   int
  508.     vibflag = 0;
  509.  
  510.   /* Play normally until inside the loop region */
  511.   while (count && (ofs <= ls)) 
  512.     {
  513.       i = (ls - ofs) / incr + 1;
  514.       if (i > count) i = count;
  515.       if (i > cc) 
  516.     {
  517.       i = cc;
  518.       vibflag = 1;
  519.     } 
  520.       else cc -= i;
  521.       count -= i;
  522.       while (i--) 
  523.     {
  524.       RESAMPLATION;
  525.       ofs += incr;
  526.     }
  527.       if (vibflag) 
  528.     {
  529.       cc = vp->vibrato_control_ratio;
  530.       incr = update_vibrato(vp, 0);
  531.       vibflag = 0;
  532.     }
  533.     }
  534.   
  535.   /* Then do the bidirectional looping */
  536.  
  537.   while (count) 
  538.     {
  539.       /* Precalc how many times we should go through the loop */
  540.       i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
  541.       if(i > count) i = count;
  542.       if(i > cc) 
  543.     {
  544.       i = cc;
  545.       vibflag = 1;
  546.     } 
  547.       else cc -= i;
  548.       count -= i;
  549.       while (i--) 
  550.     {
  551.       RESAMPLATION;
  552.       ofs += incr;
  553.     }
  554.       if (vibflag) 
  555.     {
  556.       cc = vp->vibrato_control_ratio;
  557.       incr = update_vibrato(vp, (incr < 0));
  558.       vibflag = 0;
  559.     }
  560.       if (ofs >= le) 
  561.     {
  562.       /* fold the overshoot back in */
  563.       ofs = le2 - ofs;
  564.       incr *= -1;
  565.     } 
  566.       else if (ofs <= ls) 
  567.     {
  568.       ofs = ls2 - ofs;
  569.       incr *= -1;
  570.     }
  571.     }
  572.  
  573. #else /* PRECALC_LOOPS */
  574.   /* Play normally until inside the loop region */
  575.  
  576.   if (ofs < ls)
  577.     {
  578.       while (count--)
  579.     {
  580.       if (!cc--)
  581.         {
  582.           cc=vp->vibrato_control_ratio;
  583.           incr=update_vibrato(vp, 0);
  584.         }
  585.       RESAMPLATION;
  586.       ofs += incr;
  587.       if (ofs>=ls)
  588.         break;
  589.     }
  590.     }
  591.  
  592.   /* Then do the bidirectional looping */
  593.  
  594.   if (count>0)
  595.     while (count--)
  596.       {
  597.     if (!cc--)
  598.       {
  599.         cc=vp->vibrato_control_ratio;
  600.         incr=update_vibrato(vp, (incr < 0));
  601.       }
  602.     RESAMPLATION;
  603.     ofs += incr;
  604.     if (ofs>=le)
  605.       {
  606.         /* fold the overshoot back in */
  607.         ofs = le - (ofs - le);
  608.         incr = -incr;
  609.       }
  610.     else if (ofs <= ls)
  611.       {
  612.         ofs = ls + (ls - ofs);
  613.         incr = -incr;
  614.       }
  615.       }
  616. #endif /* PRECALC_LOOPS */
  617.  
  618.   vp->vibrato_control_counter=cc;
  619.   vp->sample_increment=incr;
  620.   vp->sample_offset=ofs; /* Update offset */
  621.   return resample_buffer;
  622. }
  623.  
  624. sample_t *resample_voice(int v, int32 *countptr)
  625. {
  626.   int32 ofs;
  627.   uint8 modes;
  628.   Voice *vp=&voice[v];
  629.   
  630.   if (!(vp->sample->sample_rate))
  631.     {
  632.       /* Pre-resampled data -- just update the offset and check if
  633.          we're out of data. */
  634.       ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
  635.                          FRACTION_BITS here... */
  636.       if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
  637.     {
  638.       /* Note finished. Free the voice. */
  639.       vp->status = VOICE_FREE;
  640.       ctl->note(v);
  641.       
  642.       /* Let the caller know how much data we had left */
  643.       *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
  644.     }
  645.       else
  646.     vp->sample_offset += *countptr << FRACTION_BITS;
  647.       
  648.       return vp->sample->data+ofs;
  649.     }
  650.  
  651.   /* Need to resample. Use the proper function. */
  652.   modes=vp->sample->modes;
  653.  
  654.   if (vp->vibrato_control_ratio)
  655.     {
  656.       if ((modes & MODES_LOOPING) &&
  657.       ((modes & MODES_ENVELOPE) ||
  658.        (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
  659.     {
  660.       if (modes & MODES_PINGPONG)
  661.         return rs_vib_bidir(vp, *countptr);
  662.       else
  663.         return rs_vib_loop(vp, *countptr);
  664.     }
  665.       else
  666.     return rs_vib_plain(v, countptr);
  667.     }
  668.   else
  669.     {
  670.       if ((modes & MODES_LOOPING) &&
  671.       ((modes & MODES_ENVELOPE) ||
  672.        (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
  673.     {
  674.       if (modes & MODES_PINGPONG)
  675.         return rs_bidir(vp, *countptr);
  676.       else
  677.         return rs_loop(vp, *countptr);
  678.     }
  679.       else
  680.     return rs_plain(v, countptr);
  681.     }
  682. }
  683.  
  684. void pre_resample(Sample * sp)
  685. {
  686.   double a, xdiff;
  687.   int32 incr, ofs, newlen, count;
  688.   int16 *newdata, *dest, *src = (int16 *) sp->data;
  689.   int16 v1, v2, v3, v4, *vptr;
  690.   static const char note_name[12][3] =
  691.   {
  692.     "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
  693.   };
  694.  
  695.   ctl->cmsg(CMSG_INFO, VERB_NOISY, " * pre-resampling for note %d (%s%d)",
  696.         sp->note_to_use,
  697.         note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
  698.  
  699.   a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
  700.     ((double) (sp->root_freq) * play_mode->rate);
  701.   newlen = sp->data_length / a;
  702.   dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1));
  703.  
  704.   count = (newlen >> FRACTION_BITS) - 1;
  705.   ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
  706.  
  707.   if (--count)
  708.     *dest++ = src[0];
  709.  
  710.   /* Since we're pre-processing and this doesn't have to be done in
  711.      real-time, we go ahead and do the full sliding cubic interpolation. */
  712.   while (--count)
  713.     {
  714.       vptr = src + (ofs >> FRACTION_BITS);
  715.       v1 = *(vptr - 1);
  716.       v2 = *vptr;
  717.       v3 = *(vptr + 1);
  718.       v4 = *(vptr + 2);
  719.       xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
  720.       *dest++ = v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
  721.       xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4)));
  722.       ofs += incr;
  723.     }
  724.  
  725.   if (ofs & FRACTION_MASK)
  726.     {
  727.       v1 = src[ofs >> FRACTION_BITS];
  728.       v2 = src[(ofs >> FRACTION_BITS) + 1];
  729.       *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
  730.     }
  731.   else
  732.     *dest++ = src[ofs >> FRACTION_BITS];
  733.  
  734.   sp->data_length = newlen;
  735.   sp->loop_start = sp->loop_start / a;
  736.   sp->loop_end = sp->loop_end / a;
  737.   free(sp->data);
  738.   sp->data = (sample_t *) newdata;
  739.   sp->sample_rate = 0;
  740. }
  741.